package com.ruoyi.initialdata.tool;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.initialdata.domain.InitialDataTable1;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlParser {

    public List<InitialDataTable1> parseXmlFile(String xmlFilePath) throws Exception {
        List<InitialDataTable1> dataList = new ArrayList<>();

        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(xmlFilePath));

        Element rootElement = document.getRootElement();
        List<Element> dataElements = rootElement.elements("data");

        for (Element dataElement : dataElements) {
            // 解析 XML 中的字段并创建 InitialDataTable1 对象
            InitialDataTable1 data = new InitialDataTable1();
            data.setDataId(Long.parseLong(dataElement.elementText("data_id")));
            data.setDataIndexName(dataElement.elementText("data_index_name"));
            data.setDataType(dataElement.elementText("data_type"));

            String dataNumberStr = dataElement.elementText("data_number");
            if (dataNumberStr != null && !dataNumberStr.isEmpty()) {
                data.setDataNumber(dataNumberStr);
            }

            String dataString = dataElement.elementText("data_string");
            if (dataString != null && !dataString.isEmpty()) {
                data.setDataString(dataString);
            }

            data.setDataTime(dataElement.elementText("data_time"));
            data.setDataContent(dataElement.elementText("data_content"));

            dataList.add(data);
        }

        return dataList;
    }
}
